/** 
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.dracots;

import java.io.Serializable;
import java.util.Hashtable;

/**
 * System used tuples's types. This class is designed as enum which can 
 * be used since JDK 1.5. But for JDK 1.3 compatible reason, we do not use enum
 * facility directly.
 * 
 * @author Pan Wenhui
 */
//@SuppressWarnings("serial")
public class TupleType implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2189518995302044783L;
	private String typeName;

	public String getTypeName() {
		return typeName;
	}

	public TupleType(String typeName) {
		this.typeName = typeName;
		types.put(typeName, this);
	}

	// Map<String type, TupleType>
	static private Hashtable types = new Hashtable();
	
	/**
	 * Get the tuple type object through type name
	 * if not found, return null.
	 * @author Pan Wenhui
	 */
	static TupleType get(String typeName){
		return (TupleType) types.get(typeName);
	}

	public String toString(){
		return getTypeName();
	}
	
	/**
	 * If two type names are equal, types are equal
	 * @author Xing Jiankuan
	 */
	public boolean equals(Object o) {
		if(this == o) return true;
		if(o instanceof TupleType) {
			return this.typeName.equals(((TupleType)o).typeName);
		} else {
			return false;
		}
	}
	
	@Override
	public int hashCode() {
		return typeName.hashCode();
	}
}
